package com.yunzong.wifi.netconfig;

import android.content.Context;
import android.os.SystemClock;
import android.util.Log;

import com.yunzong.wifi.netconfig.protocol.CRC8;
import com.yunzong.wifi.netconfig.socket.UDPMultiClient;
import com.yz.jdd.base.utils.LogUtils;

import java.util.Arrays;


public class SmartConfig implements INetConfig {
    private boolean connecting;
    private Context context;

    public SmartConfig(Context context) {
        this.context = context;
    }

    @Override
    public void start(String ssid, String pwd) {
        if (connecting)
            return;
        connecting = true;
        final UDPMultiClient udpClient = new UDPMultiClient();
        try {
            final Information info = new Information(context, ssid, pwd);
            new Thread(new Runnable() {
                @Override
                public void run() {
                    final int LeadCode = 4;
                    int count = 1;
                    byte[] SendData = new byte[info.DataPackageSum * 3];
                    for (int i = 0, j = 0; i < info.info.length; j = j + 3) {
                        if (i < LeadCode) {
                            SendData[j] = (byte) count;
                            SendData[j + 1] = (byte) (info.info[i] & 0xff);
                            byte[] crc = new byte[1];
                            crc[0] = (byte) (info.info[i] & 0xff);
                            SendData[j + 2] = CRC8.CRC8(crc);
                            i++;
                        } else {
                            SendData[j] = (byte) count;
                            SendData[j + 1] = (byte) (info.info[i] & 0xff);
                            SendData[j + 2] = (byte) (info.info[i + 1] & 0xff);
                            i = i + 2;
                        }
                        count++;
                    }
                    String[] UdpIpData = new String[3];
                    String[] IP = new String[info.DataPackageSum];
                    for (int j = 0; j < (SendData.length / 3); j++) {
                        for (int i = j * 3; i < j * 3 + 3; i = i + 3) {
                            UdpIpData[0] = String.valueOf(SendData[i] & 0xff);
                            UdpIpData[1] = String.valueOf(SendData[i + 1] & 0xff);
                            UdpIpData[2] = String.valueOf(SendData[i + 2] & 0xff);
                        }
                        IP[j] = "239." + UdpIpData[0] + "." + UdpIpData[1] + "." + UdpIpData[2];
                    }
                    for (int i = 0; i < SendData.length; i++) {

                    }
                    Log.e("lkf",Arrays.toString(SendData));
                    LogUtils.e("=== "+Arrays.toString(SendData));
                    while (connecting) {
                        byte[] Data = new byte[1];
                        for (int i = 0; i < 3; i++) {
                            for (int j = 0; j < LeadCode; j++) {
                                udpClient.send(Data, IP[j]);
                                SystemClock.sleep(10);
                            }
                        }
                        for (int i = LeadCode; i < IP.length; i++) {
                            udpClient.send(Data, IP[i]);
                            SystemClock.sleep(10);
                            if ((i % 5) == 0) {
                                udpClient.send(Data, IP[0]);
                                SystemClock.sleep(10);
                            }
                        }
                    }
                }
            }).start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void stop() {
        if (connecting) {
            connecting = false;
        }
    }
}
